<script setup>
import { ref, onMounted } from 'vue'
import { getDashboardOverview } from '../api/book'

const overview = ref({
  progress: 0,
  todayCount: 0,
  targetCount: 10000,
  completedCount: 0,
  dbStatus: 'normal',
  trendData: []
})

const fetchData = async () => {
  try {
    const res = await getDashboardOverview()
    overview.value = res.data
  } catch (error) {
    console.error('获取仪表盘数据失败:', error)
  }
}

onMounted(() => {
  fetchData()
  // 每30秒刷新一次数据
  setInterval(fetchData, 30000)
})
</script>

<template>
  <div class="status-cards">
    <el-card shadow="hover" class="status-card">
      <template #header>
        <div class="card-header">
          <span>爬虫进度</span>
        </div>
      </template>
      <el-progress 
        :percentage="overview.progress" 
        :stroke-width="16" 
        :text-inside="true"
        status="success"
      />
      <div class="progress-info">
        <span>已完成: {{ overview.completedCount }} / {{ overview.targetCount }}</span>
        <span>今日新增: {{ overview.todayCount }}</span>
      </div>
    </el-card>

    <el-card shadow="hover" class="status-card">
      <template #header>
        <div class="card-header">
          <span>数据库状态</span>
        </div>
      </template>
      <div class="db-status" :class="overview.dbStatus">
        <el-icon :size="40">
          <component :is="overview.dbStatus === 'normal' ? 'CircleCheck' : 'Warning'" />
        </el-icon>
        <span>{{ overview.dbStatus === 'normal' ? '运行正常' : '出现异常' }}</span>
      </div>
    </el-card>

    <el-card shadow="hover" class="status-card">
      <template #header>
        <div class="card-header">
          <span>7天趋势</span>
        </div>
      </template>
      <div class="trend-chart">
        <div 
          v-for="(item, index) in overview.trendData" 
          :key="index" 
          class="trend-item"
          :style="{ height: `${item.count / Math.max(...overview.trendData.map(i => i.count)) * 100}%` }"
        >
          <span class="count">{{ item.count }}</span>
          <span class="date">{{ item.date.split('-').slice(1).join('/') }}</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.status-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}

.status-card {
  height: 180px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 14px;
  color: #666;
}

.db-status {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100px;
}

.db-status.normal {
  color: #67C23A;
}

.db-status.error {
  color: #F56C6C;
}

.db-status span {
  margin-top: 10px;
  font-size: 16px;
}

.trend-chart {
  display: flex;
  height: 100px;
  align-items: flex-end;
  gap: 10px;
}

.trend-item {
  flex: 1;
  background-color: #409EFF;
  border-radius: 4px 4px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 5px;
  color: white;
  font-size: 12px;
  min-height: 20px;
}

.trend-item .count {
  font-weight: bold;
}

.trend-item .date {
  margin-top: 5px;
}
</style>